<?php
$link = mysqli_connect("localhost","root","123456","transaction");
// $link = mysqli_connect("sql108.xiaopangkj.space","xiaop_31205296","chen123.","xiaop_31205296_data");
$ver = "SELECT * FROM area WHERE area_name='{$_POST['area_name']}'";
$res = mysqli_query($link,$ver);
$one = mysqli_fetch_assoc($res);
//区服绑定语句
$sql = "SELECT * FROM server LEFT JOIN area ON server.aid=area.id WHERE area.id = '{$one['id']}'";
$data = [];
//验证登录语句
$login = "SELECT * FROM USER WHERE user_id='{$_POST['user_id']}'";
//登录
if($_POST['user_id']!=null){
	$loginres = mysqli_query($link,$login);
	$loginreturn =mysqli_fetch_assoc($loginres);
	if($loginreturn['password']==$_POST['password']){
		echo json_encode($loginreturn);
	}
}
//区服绑定
if($_POST['area_name']!=null){
$res2 = mysqli_query($link,$sql);
while($two =mysqli_fetch_assoc($res2)){
	$data[] = $two;
}

echo json_encode($data);	
}
//发布
if($_POST['title']!=null){
	$_POST['nowtime'] = date("Y/m/d H:i:s",time());
	//发布信息语句
	$rel = "INSERT INTO inform_list (server_name,type,title,price,contace,nowtime,u_id,user_name) VALUES ('{$_POST['server_name']}','{$_POST['type']}','{$_POST['title']}','{$_POST['price']}','{$_POST['contace']}','{$_POST['nowtime']}','{$_POST['u_id']}','{$_POST['user_name']}') ";
	$res3 = mysqli_query($link,$rel);
	// echo json_encode($rel);
	if(mysqli_affected_rows($link)){
		$data['info']="发布成功";
		$data['status'] = 1;
		echo json_encode($data);
	}else{
		$data['info']="发布失败";
		$data['status'] = 0;
		echo json_encode($data);
	}
}
//html加载完成渲染
if($_POST['cmd']){
	$ren="SELECT * FROM inform_list ORDER BY nowtime DESC";
	$res4 = mysqli_query($link,$ren);
	while($rendom=mysqli_fetch_assoc($res4)){
		$data[] = $rendom;
		// echo json_encode($_POST);
	}
	echo json_encode($data);
}
//联系方式交换
if($_POST['call']){
	$nowtime = date("Y/m/d",time());
	$call = "INSERT INTO calllist (u_id,u_contace,c_contace,c_id,l_id,nowtime) VALUES ('{$_POST['u_id']}','{$_POST['u_contace']}','{$_POST['c_contace']}','{$_POST['c_id']}','{$_POST['l_id']}','$nowtime')";
	$lid= "SELECT * FROM calllist WHERE l_id='{$_POST['l_id']}' AND cid='{$_POST['c_id']}' ";
	$lidres = mysqli_query($link,$lid);
	$lidone = mysqli_fetch_assoc($lidres);
	//判断是否已经发送过
	if($lidone!=null){
		$data["info"] = "你已经发送过了";
		$data["status"] = 0;
		echo json_encode($data);
		
	}else{
		mysqli_query($link,$call);
		$data["info"] = "发送成功";
		$data["status"] = 1;
		echo json_encode($data);
	}
}
if($_POST['pre']){
	$pre = "SELECT * FROM inform_list WHERE u_id='{$_POST['u_id']}'";
	$preres = mysqli_query($link,$pre);
	while($preone=mysqli_fetch_assoc($preres)){
		$data[] = $preone;
		// echo json_encode($_POST);
	}
	echo json_encode($data);
}
//买家信息
if($_POST['buy']){
	// SELECT * FROM book LEFT JOIN book_type ON book.type_id=book_type.type_id;
	$buy = "SELECT * FROM inform_list LEFT JOIN calllist ON calllist.l_id=inform_list.id WHERE calllist.u_id='{$_POST['u_id']}'";
	// SELECT * FROM calllist LEFT JOIN inform_list ON calllist.l_id=inform_list.id WHERE calllist.u_id='39546'
	$buyres = mysqli_query($link,$buy);
	while($buyone=mysqli_fetch_assoc($buyres)){
		$data[] = $buyone;
		// echo json_encode($_POST);
	}
	echo json_encode($data);
}
//卖家信息
if($_POST['sell']){
	// SELECT * FROM book LEFT JOIN book_type ON book.type_id=book_type.type_id;
	$sell = "SELECT * FROM inform_list LEFT JOIN calllist ON calllist.l_id=inform_list.id WHERE calllist.c_id='{$_POST['u_id']}'";
	// SELECT * FROM calllist LEFT JOIN inform_list ON calllist.l_id=inform_list.id WHERE calllist.u_id='39546'
	$sellres = mysqli_query($link,$sell);
	while($sellone=mysqli_fetch_assoc($sellres)){
		$data[] = $sellone;
		// echo json_encode($_POST);
	}
	echo json_encode($data);
}
//获取页数
if($_POST['count']){
	$pagecount = $_POST['count'];
	$pagesql = "SELECT COUNT(*) AS total FROM inform_list";
	
	$pageres =  mysqli_query($link,$pagesql);
	
	$arr =  mysqli_fetch_assoc($pageres);
	
	// print_r($pagecount);
	$total = $arr['total'];
	
	// 计算页码
	$p = ceil( $total/$pagecount);
	// echo $p;
	$info['p'] = $p;
	echo json_encode($info);
}




?>